<script lang="ts">
import { ElIcon } from 'element-plus';
import { createVNode, defineComponent, h, resolveComponent } from 'vue';
import svgIcon from './svg-icon.vue';

const LOCAL_ICON_PREFIX = 'local-icon-';

const EL_ICON_PREFIX = 'el-icon-';

export default defineComponent({
  props: {
    name: {
      type: String,
      required: true,
    },
    size: {
      type: [String, Number],
      default: '14px',
    },
    color: {
      type: String,
      default: 'inherit',
    },
  },
  setup(props) {
    return () => {
      // el-icon
      if (props.name.indexOf(EL_ICON_PREFIX) === 0) {
        return createVNode(
          ElIcon,
          { size: props.size, color: props.color },
          {
            default: () => createVNode(resolveComponent(props.name.replace(EL_ICON_PREFIX, ''))),
          },
        );
      }
      // 本地icon
      if (props.name.indexOf(LOCAL_ICON_PREFIX) === 0) {
        return h('i', { class: ['local-icon'] }, createVNode(svgIcon, { ...props }));
      }
      // 如果name不符合预期的前缀，返回null
      return null;
    };
  },
});
</script>
